package com.hnichr.ump.build7.controller;

import com.hnichr.ump.build7.pojo.Plate;
import com.hnichr.ump.build7.service.StopService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/stop/")
public class StopController {


    @Autowired
    private StopService stopService;

    @GetMapping("v1/list")
    public Result<List<Plate>> list(@Parameter(description="查询参数") Criteria criteria){
        List<Plate> list = stopService.writeListQuery(criteria);
        return Result.OK(list);
    }

    @GetMapping("v1/page")
    public Result<Page<Plate>> page(@Parameter(description="查询参数") Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo){
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<Plate> list = stopService.writeListPage(page, criteria);
        return Result.OK(list);
    }

    @PostMapping("v1/add")
    public Result<Plate> add(@RequestBody Plate plate) {
        List<Plate> plateList = stopService.writeListQuery(plate.getPlate());
        if(CollectionUtils.isNotEmpty(plateList)){
            return Result.error("车牌已存在");
        }
        boolean result = stopService.writeListAddOrUpdate(plate.getPlate(),plate.getContext(),plate.getEnableTime(),plate.getOverdueTime(),1);
        if(result){
            return Result.OK(plate);
        }
        else{
            return Result.error("添加失败");
        }
    }

    @PutMapping("v1/edit")
    public Result<Plate> edit(@RequestBody Plate plate) {
        boolean result = stopService.writeListAddOrUpdate(plate.getPlate(),plate.getContext(),plate.getEnableTime(),plate.getOverdueTime(),1);
        if(result){
            return Result.OK(plate);
        }
        else{
            return Result.error("添加失败");
        }
    }

    @DeleteMapping("v1/remove/{ids}")
    @Operation(summary = "根据车牌删除白名单", description = "根据车牌删除白名单", method = "DELETE")
    public Result<Plate> remove(@PathVariable(name = "ids", required = true) String ids) {
        if(StringUtils.isBlank(ids)){
            return Result.error("必须输入车牌");
        }
        String[] plateArray = ids.split(",");
        boolean result = stopService.writeListDel(plateArray);
        if(result){
            return Result.OK();
        }
        else {
            return Result.error("删除失败");
        }
    }

    @GetMapping("v1/enableOrDisable/{plate}")
    public Result<String> enableOrDisable(@PathVariable(name = "plate", required = true) String plate){
        List<Plate> plateList = stopService.writeListQuery(plate);
        if(CollectionUtils.isEmpty(plateList)&&CollectionUtils.size(plateList)>1){
            return Result.error("车牌不合法");
        }
        Plate plateVo = plateList.get(0);

        boolean result = stopService.writeListAddOrUpdate(plate,plateVo.getContext(),plateVo.getEnableTime(),plateVo.getOverdueTime(),plateVo.getEnable()==1?0:1);
        if(result){
            return Result.OK();
        }
        else {
            return Result.error("操作失败");
        }
    }

    @GetMapping("v1/open")
    public Result<String> open(){
        boolean result = stopService.open();
        if(result){
            return Result.OK();
        }
        else{
            return Result.error("操作失败");
        }
    }

    @GetMapping("v1/snapshot")
    public Result<String> snapshot(){
        String imageBase64 = stopService.snapshot();
        return Result.OK(imageBase64);
    }

}
